const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const { merge } = require('webpack-merge');
const TerserPlugin = require("terser-webpack-plugin");
const JavaScriptObfuscator = require('webpack-obfuscator');

let common = {
  entry: {
    //'main': './src/index.ts',
    'export': './src/export.ts'
  },
  mode: 'development',
  devtool: 'inline-source-map',
  plugins: [
    new CopyPlugin({
      patterns: [
        // {
        //   from: path.resolve(__dirname, 'node_modules/@i3yun/viewer'),
        //   to: path.resolve(__dirname, 'dist/viewer'),
        // },
        {
          from: path.resolve(__dirname, 'test'),
          to: path.resolve(__dirname, 'dist'),
        },
      ],
    }),
  ],
  output: {
    clean: true,
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
  },
  module: {
    // 自定义解析器 https://webpack.js.org/guides/asset-management/
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        include: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'test')],
        // exclude: /node_modules/,
      },
      {
        test: /\.html$/,
        use: ['text-loader'],
      },
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['url-loader']
      },
    ],
  },
  devServer: {
    contentBase: './dist/',
    port: 8091,
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    symlinks: false,
  },
};
let devConfig = {
  mode: 'development',
  devtool: 'inline-source-map',
};


let proConfig = {
  mode: 'production',
  devtool: 'nosources-source-map',
  plugins: [
    new JavaScriptObfuscator({
      rotateStringArray: true,
    }, [])
  ],
  optimization: {
    minimize: true,
      minimizer: [new TerserPlugin()],
  },
};
var dev = process.argv.indexOf('production') < 0;
console.log('dev:' + (dev ? 'devConfig' : 'proConfig'));
module.exports = merge(common, dev ? devConfig : proConfig);
